package com.util.security.mtoc;

/**
 * @Author: commander
 * @Description: 维吉尼亚加密/解密算法
 * @Version: 1.0
 * @Date: 2016/12/11 18:06
 */
public class Virginia {
	public Virginia() {
	}

	//输入明文和密钥，用输入的密钥对明文进行加密
	public void vga(String k, String m) {
		int i;
		char[] c = new char[100];
		char[] k1 = new char[100];
		//构造密钥对照表
		for (i = 0; i < k.length(); i++) {
			if (k.charAt(i) >= 'a' && k.charAt(i) <= 'z') {
				k1[i] = (char) (k.charAt(i) - 97);
			}
			if (k.charAt(i) >= 'A' && k.charAt(i) <= 'Z') {
				k1[i] = (char) (k.charAt(i) - 65);
			}
		}

		//加密
		for (i = 0; i < m.length(); i++) {
			if (m.charAt(i) >= 'a' && m.charAt(i) <= 'z') {
				c[i] = (char) ((m.charAt(i) - 97 + k1[i % k.length()]) % 26 + 97);
			}
			if (m.charAt(i) >= 'A' && m.charAt(i) <= 'Z') {
				c[i] = (char) ((m.charAt(i) - 65 + k1[i % k.length()]) % 26 + 65);
			}
		}

//输出密文
		System.out.print("密文：");
		for (i = 0; i < c.length; i++) {
			System.out.print(c[i]);
		}
		System.out.println("");
	}

//	public static void main(String[] args) {
//		System.out.print("请输入明文：");
//		String m = MyInput.readString();
//		System.out.print("请输入key：");
//		String k = MyInput.readString();
//		new Virginia().vga(k, m);
//	}
}
